<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Discount_config_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $month_period = 0;
	var $percent_discount = 0;
	var $active = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id = $input;
	}
	function setMonthPeriod($input){
		$this->month_period = $input;
	}
	function setPercentDiscount($input){
		$this->percent_discount = $input;
	}
	
	function setActive($input){
		$this->active = $input;
	}
	function getId(){
		return $this->id;
	}
	function getMonthPeriod(){
		return $this->month_period;
	}
	function getPercentDiscount(){
		return $this->percent_discount;
	}
	function getActive(){
		return $this->active;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_DISCOUNT_CONFIG)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->month_period = $result[0]->month_period;
			$this->percent_discount = $result[0]->percent_discount;
			$this->active = $result[0]->active;
			return $this;
		}
		return false;
	}
	
	function getList($limit = array()){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(count($limit) >0){
			$result = $ci->db->limit($limit[0], $limit[1]);
		}
		$ci->db->order_by('id', 'ASC');
		return $ci->db->get(TBL_DISCOUNT_CONFIG)->result();
	}
	function count(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		return $ci->db->count_all_results(TBL_DISCOUNT_CONFIG);
	}
	
	function save(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('month_period',$this->getMonthPeriod());
		$ci->db->set('percent_discount',$this->getPercentDiscount());
		$ci->db->set('active',$this->getActive());
		$ci->db->insert(TBL_DISCOUNT_CONFIG);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('month_period',$this->getMonthPeriod());
		$ci->db->set('percent_discount',$this->getPercentDiscount());
		$ci->db->set('active',$this->getActive());
		return $ci->db->where('id', $this->id)->update(TBL_DISCOUNT_CONFIG);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_DISCOUNT_CONFIG);
	}
	//end class
}